package org.cgs.web.server.scheduler;

import java.io.IOException;

import javax.servlet.http.*;


public final class Scheduler extends HttpServlet
{
    private static final long serialVersionUID = 1L;

    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException
    {
        String courseCode = (String)request.getSession().getAttribute("courseCode");
        int maxTime = Integer.parseInt(request.getParameter("maxTime") == null ? "5" : request.getParameter("maxTime"));
        boolean vehicleConstraint = request.getParameter("vehicle") == null ? false : true;
        boolean subjectKnowledgeConstraint = request.getParameter("subjectKnowledge") == null ? false : true;
        boolean teachingExperienceConstraint = request.getParameter("teachingExprience") == null ? false : true;
        boolean plusOneConstraint = request.getParameter("plusOne") == null ? false : true;

        // Start the thread
        SchedulingAlgorithm thread = new SchedulingAlgorithm(courseCode, maxTime, vehicleConstraint, subjectKnowledgeConstraint, teachingExperienceConstraint, plusOneConstraint);
        thread.start();

        // Wait until the thread is done executing
        while (!thread.isDoneScheduling())
        {
        	// Do nothing and wait
        	System.currentTimeMillis();
        }

        // Print the XML response
        response.getOutputStream().print(thread.getOutput());
    } // End method
} // End class
